//
//  RegisterViewController.m
//  carSecretary
//
//  Created by huangliru on 14-9-4.
//  Copyright (c) 2014年 huangliru. All rights reserved.
//

#import "RegisterViewController.h"
#import "NextViewController.h"
#import <QRCodeReader.h>
#import <MultiFormatOneDReader.h>

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    
    //创建一个导航栏
    UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 18, 320, 44)];
    //创建一个导航栏集合
    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];
    //创建一个左边按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(backButton)];
    //设置导航栏内容
    [navigationItem setTitle:@"注册账号"];
    //把导航栏集合添加入导航栏中，设置动画关闭
    [bar pushNavigationItem:navigationItem animated:NO];
    //把左按钮添加入导航栏集合中
    [navigationItem setLeftBarButtonItem:leftButton];
    bar.backgroundColor = [UIColor grayColor];
    [control addSubview:bar];
    
    UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(20, 90, 110, 40)];
    nameLable.text = @"用  户  名：";
    nameLable.textColor = [UIColor blackColor];
    [control addSubview:nameLable];
    
    UILabel *pswd1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 150, 110, 40)];
    pswd1.textColor = [UIColor blackColor];
    pswd1.text = @"密       码：";
    [control addSubview:pswd1];
    
    UILabel *pswd2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 210, 110, 40)];
    pswd2.textColor = [UIColor blackColor];
    pswd2.text = @"确认密码：";
    [control addSubview:pswd2];
    
    UILabel *deviceID = [[UILabel alloc]initWithFrame:CGRectMake(20, 270, 110, 40)];
    deviceID.textColor = [UIColor blackColor];
    deviceID.text = @"设    备ID：";
    [control addSubview:deviceID];
    
    UILabel *mac = [[UILabel alloc]initWithFrame:CGRectMake(20, 390, 110, 40)];
    mac.textColor = [UIColor blackColor];
    mac.text = @"MAC地址：";
    [control addSubview:mac];
    
    
    // 初始化账号输入
    self.userNameText = [[UITextField alloc]initWithFrame:CGRectMake(126 , 95, 174, 30)];
    //让字体大小自适应控件的区域
    [self.userNameText setBorderStyle:UITextBorderStyleRoundedRect];
    [self.userNameText  setAdjustsFontSizeToFitWidth:YES];
    self.userNameText.placeholder = @"请输入您的用户名";
    //设置委托对象
    self.userNameText.delegate = self;
    //添加到本视图
    [control addSubview: self.userNameText];
    
    
    self.passwordText = [[UITextField alloc]initWithFrame:CGRectMake(126, 155, 174, 30)];
    //让字体大小自适应控件的区域
    [self.passwordText setBorderStyle:UITextBorderStyleRoundedRect];
    [self.passwordText  setAdjustsFontSizeToFitWidth:YES];
    self.passwordText.placeholder = @"请输入您的密码";
    self.passwordText.secureTextEntry = YES;
    //设置委托对象
    self.passwordText.delegate = self;
    //添加到本视图
    [control addSubview: self.passwordText];
    
    self.surepasswordText = [[UITextField alloc]initWithFrame:CGRectMake(126, 215, 174, 30)];
    //让字体大小自适应控件的区域
    [self.surepasswordText setBorderStyle:UITextBorderStyleRoundedRect];
    [self.surepasswordText  setAdjustsFontSizeToFitWidth:YES];
    self.surepasswordText.placeholder = @"请再次输入您的密码";
    self.surepasswordText.secureTextEntry = YES;
    //设置委托对象
    self.surepasswordText.delegate = self;
    //添加到本视图
    [control addSubview: self.surepasswordText];
    
    self.deviceIDText = [[UITextField alloc]initWithFrame:CGRectMake(126, 275, 174, 30)];
    //让字体大小自适应控件的区域
    [self.deviceIDText setBorderStyle:UITextBorderStyleRoundedRect];
    [self.deviceIDText  setAdjustsFontSizeToFitWidth:YES];
    self.deviceIDText.placeholder = @"请扫描设备的二维码";
    //设置委托对象
    self.deviceIDText.delegate = self;
    //添加到本视图
    [control addSubview: self.deviceIDText];
    
    
    
    self.macText = [[UITextField alloc]initWithFrame:CGRectMake(126, 395, 174, 30)];
    self.macText.tag = 12;
    //让字体大小自适应控件的区域
    [self.macText setBorderStyle:UITextBorderStyleRoundedRect];
    [self.macText  setAdjustsFontSizeToFitWidth:YES];
    //设置委托对象
    self.macText.delegate = self;
    //添加到本视图
    [control addSubview: self.macText];
//    // 请求服务器获取设备唯一标识
//    NSString * urlString =@"http://192.168.1.137:8089/CommunityWS/ServicesBizImplPort/login";
//    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
//    [request setPostValue:usernameField.text forKey:@"macID"];
//    [request setRequestMethod:@"POST"]; [request startAsynchronous];
    
    
    
    
    // 扫描二维码按钮
    self.scanButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.scanButton.frame = CGRectMake(126, 330, 174, 35);
    self.scanButton.tintColor = [UIColor blackColor];
    [self.scanButton setTitle:@"扫描设备ID" forState:UIControlStateNormal];
    [self.scanButton setBackgroundImage:[UIImage imageNamed:@"213.png"] forState:UIControlStateNormal];
    [self.scanButton addTarget:self action:@selector(scanZXing) forControlEvents:UIControlEventTouchUpInside];
    [control addSubview:self.scanButton];
    
    // 下一步操作
    self.nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.nextButton.frame = CGRectMake(91, 462, 139, 50);
    self.nextButton.tintColor = [UIColor blackColor];
    [self.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextButton setBackgroundImage:[UIImage imageNamed:@"213.png"] forState:UIControlStateNormal];
    [self.nextButton addTarget:self action:@selector(nextView) forControlEvents:UIControlEventTouchUpInside];
    [control addSubview:self.nextButton];
    
    [control addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchDown];
    self.view = control;
}
//实现textfielddelegate方法
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView  beginAnimations:@"aa" context:nil];
    [UIView setAnimationDuration:0.3];
    if ([textField tag] == 12)
    {
        CGRect frame = self.view.frame;
        frame.origin.y -= 100;
        self.view.frame = frame;
    }
    [UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [UIView beginAnimations:@"aa" context:nil];
    self.view.frame = CGRectMake(0, 0, 320, 560);
    [UIView setAnimationDuration:0.3];
    [UIView commitAnimations];
    
    
    [self.userNameText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.surepasswordText resignFirstResponder];
    [self.deviceIDText resignFirstResponder];
    [self.macText resignFirstResponder];
    return YES;
}

// 扫描二维码按钮
-(void)scanZXing
{
    ZXingWidgetController *widecontroller=[[ZXingWidgetController alloc]initWithDelegate:self showCancel:YES OneDMode:NO];
    widecontroller.view.backgroundColor = [UIColor blackColor];
    NSMutableSet *readers = [[NSMutableSet alloc]init];
    QRCodeReader *qrcodereader =  [[QRCodeReader alloc]init];
    [readers addObject:qrcodereader];
    widecontroller.readers = readers;
    [self presentViewController:widecontroller animated:YES completion:nil];
}
- (void)outPutResult:(NSString *)result
{
    NSLog(@"result:%@", result);
    self.deviceIDText.text = result;
}
#pragma mark - ZXingDelegate

- (void)zxingController:(ZXingWidgetController *)controller didScanResult:(NSString *)result
{
    [self dismissViewControllerAnimated:YES completion:^{[self outPutResult:result];}];
}

- (void)zxingControllerDidCancel:(ZXingWidgetController *)controller
{
    [self dismissViewControllerAnimated:YES completion:^{NSLog(@"cancel!");}];
}
// 下一步实现方法
-(void)nextView
{
    NextViewController *nextVC = [[NextViewController alloc]init];
    [self presentViewController:nextVC animated:NO completion:nil];
}
// 获取手机mac地址
//-(NSString *)getMacAddress
//{
//    
//    
//    return macAddressString;
//}
// 导航栏返回按钮
-(void)backButton
{
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
